﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using ZOIL.Library.Devices;
using ZOIL.Framework.Core;

namespace Sample.RenderPerformance
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindowLoaded;
        }

        void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            CreateObjects((int)ElementsSlider.Value);
        }

        private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!IsLoaded) return;

            if (e.NewValue > e.OldValue)
            {
                CreateObjects((int)(e.NewValue - e.OldValue));
            }
            else if (e.NewValue < e.OldValue)
            {
                RemoveObjects((int)(e.OldValue - e.NewValue));
            }
        }

        private void CreateObjects(int count)
        {
            ProgressIndicator.Visibility = Visibility.Visible;

            for (var i = 0; i < count; i++)
            {
                var control = new MySemanticZoomControl();
                LandscapeCanvas.Children.Add(control);
            }

            ProgressIndicator.Visibility = Visibility.Collapsed;
        }

        private void RemoveObjects(int count)
        {
            ProgressIndicator.Visibility = Visibility.Visible;

            for (var i = 0; i < count; i++)
            {
                var objects = LandscapeCanvas.Children.Count;
                LandscapeCanvas.Children.RemoveAt(objects - 1);
            }

            ProgressIndicator.Visibility = Visibility.Collapsed;
        }
    }
}
